//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "JointStochasticProcess.h"
using namespace Cephei::QL::Processes;
#include <gen/QL/Math/Array.h>
#include <gen/QL/StochasticProcess.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (boost::shared_ptr<QuantLib::JointStochasticProcess>& childNative, Object^ owner) : CStochasticProcess(CJointStochasticProcess::typeid)
{
#ifdef HANDLE
	_phJointStochasticProcess = NULL;
#endif
	_ppJointStochasticProcess = &childNative;
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
}
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (QuantLib::JointStochasticProcess& childNative, Object^ owner) : CStochasticProcess(CJointStochasticProcess::typeid)
{
#ifdef HANDLE
	_phJointStochasticProcess = NULL;
#endif
	_ppJointStochasticProcess = new boost::shared_ptr<QuantLib::JointStochasticProcess> (&childNative);
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
    _JointStochasticProcessOwner = owner;
    _StochasticProcessOwner = owner;
}

Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (CJointStochasticProcess^ copy) : CStochasticProcess(CJointStochasticProcess::typeid)
{
#ifdef HANDLE
	_phJointStochasticProcess = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppJointStochasticProcess = new boost::shared_ptr<QuantLib::JointStochasticProcess> (copy->GetShared());
        _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
    }
}
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (PLATFORM::Type^ t) : CStochasticProcess(CJointStochasticProcess::typeid)
{
#ifdef HANDLE
	_phJointStochasticProcess = NULL;
#endif
	if (!t->IsSubclassOf(CJointStochasticProcess::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (QuantLib::Handle<QuantLib::JointStochasticProcess>& childNative, Object^ owner)  : CStochasticProcess(CJointStochasticProcess::typeid)
{
	_phJointStochasticProcess = &childNative;
	_ppJointStochasticProcess = &static_cast<boost::shared_ptr<QuantLib::JointStochasticProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
    _JointStochasticProcessOwner = owner;
}
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (QuantLib::Handle<QuantLib::JointStochasticProcess> childNative)  : CStochasticProcess(CJointStochasticProcess::typeid)
{
	_phJointStochasticProcess = &childNative;
	_ppJointStochasticProcess = &static_cast<boost::shared_ptr<QuantLib::JointStochasticProcess>>(childNative.currentLink());
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
}
#endif
#ifdef STRUCT
Cephei::QL::Processes::CJointStochasticProcess::CJointStochasticProcess (QuantLib::JointStochasticProcess childNative)  : CStochasticProcess(CJointStochasticProcess::typeid)
{
#ifdef HANDLE
	_phJointStochasticProcess = NULL;
#endif
	_ppJointStochasticProcess = new boost::shared_ptr<QuantLib::JointStochasticProcess> (new QuantLib::JointStochasticProcess (childNative));
    _ppStochasticProcess = new boost::shared_ptr<QuantLib::StochasticProcess> (boost::dynamic_pointer_cast<QuantLib::StochasticProcess> (*_ppJointStochasticProcess));
}
#endif

Cephei::QL::Processes::CJointStochasticProcess::~CJointStochasticProcess ()
{
    if (_ppJointStochasticProcess != NULL)
    {
	    delete _ppJointStochasticProcess;
        _ppJointStochasticProcess = NULL;
    }
}
Cephei::QL::Processes::CJointStochasticProcess::!CJointStochasticProcess ()
{
    if (_ppJointStochasticProcess != NULL)
    {
	    delete _ppJointStochasticProcess;
    }
}
QuantLib::JointStochasticProcess& Cephei::QL::Processes::CJointStochasticProcess::GetReference ()
{
    if (_ppJointStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return **_ppJointStochasticProcess;
}
boost::shared_ptr<QuantLib::JointStochasticProcess>& Cephei::QL::Processes::CJointStochasticProcess::GetShared ()
{
    if (_ppJointStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return *_ppJointStochasticProcess;
}
QuantLib::JointStochasticProcess* Cephei::QL::Processes::CJointStochasticProcess::GetPointer ()
{
    if (_ppJointStochasticProcess == NULL) throw REFNEW NativeNullException ();
	return &**_ppJointStochasticProcess;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::JointStochasticProcess>& Cephei::QL::Processes::CJointStochasticProcess::GetHandle ()
{
	if (_phJointStochasticProcess == NULL)
	{
		_phJointStochasticProcess = new Handle<QuantLib::JointStochasticProcess> (*_ppJointStochasticProcess);
	}
	return *_phJointStochasticProcess;
}
#endif
bool Cephei::QL::Processes::CJointStochasticProcess::HasNative () 
{
	return (_ppJointStochasticProcess != NULL);
}

Cephei::Core::IVector<Cephei::QL::IStochasticProcess^>^ Cephei::QL::Processes::CJointStochasticProcess::Constituents::get ()
{
    try
    {
    	std::vector<boost::shared_ptr<QuantLib::StochasticProcess> > & _rv = (std::vector<boost::shared_ptr<QuantLib::StochasticProcess> > &)(*_ppJointStochasticProcess)->constituents ( );   
        CoVector<Cephei::QL::IStochasticProcess^>^ _nrv = REFNEW CoVector<Cephei::QL::IStochasticProcess^>(REFNEW CStochasticProcessVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Processes::CJointStochasticProcess::CorrelationIsStateDependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppJointStochasticProcess)->correlationIsStateDependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Processes::CJointStochasticProcess::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppJointStochasticProcess)->factors ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CJointStochasticProcess::Numeraire (Double t, Cephei::QL::Math::IArray^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
        _Cx = safe_cast<CArray^> (x);
        _Cx->Lock();
        QuantLib::Array& _x = static_cast<QuantLib::Array&> (_Cx->GetReference ()); 
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppJointStochasticProcess)->numeraire ( _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
Cephei::QL::Processes::IJointStochasticProcess^ Cephei::QL::Processes::CJointStochasticProcess::PreEvolve (Double t0, Cephei::QL::Math::IArray^ x0, Double dt, Cephei::QL::Math::IArray^ dw)
{
    CArray^ _Cx0;
    CArray^ _Cdw;
    try
    {
        QuantLib::Time _t0 = (QuantLib::Time)ValueHelper::Convert (t0); //a
        _Cx0 = safe_cast<CArray^> (x0);
        _Cx0->Lock();
        QuantLib::Array& _x0 = static_cast<QuantLib::Array&> (_Cx0->GetReference ()); 
        QuantLib::Time _dt = (QuantLib::Time)ValueHelper::Convert (dt); //a
        _Cdw = safe_cast<CArray^> (dw);
        _Cdw->Lock();
        QuantLib::Array& _dw = static_cast<QuantLib::Array&> (_Cdw->GetReference ()); 
    	(*_ppJointStochasticProcess)->preEvolve ( _t0,  _x0,  _dt,  _dw );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx0 != nullptr) _Cx0->Unlock();
        if (_Cdw != nullptr) _Cdw->Unlock();
    }
}
UInt64 Cephei::QL::Processes::CJointStochasticProcess::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppJointStochasticProcess)->size ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Processes::CJointStochasticProcess::Time (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //a
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppJointStochasticProcess)->time ( _date );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Processes::IJointStochasticProcess^ Cephei::QL::Processes::CJointStochasticProcess::Update::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppJointStochasticProcess)->update ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

